from django.db import models
from db.base_model import BaseModel
from db.base_manager import BaseManager
# Create your models here.

class MessageManager(BaseManager):
    def add_message(self, name, email, address, message, account):
        obj = self.create_one_object(name=name, email=email, address=address, message=message, account_id=account)
        return obj

class Message(BaseModel):

    # TextField和CharField都是文本样式，基本上没有太大差别，不过TextField理论上是无限长的，CharField需要指定长度
    name = models.TextField()
    email = models.CharField(max_length=50)
    address = models.CharField(max_length=100)
    message = models.TextField()
    created_time = models.DateTimeField(auto_now_add=True)

    account = models.ForeignKey('bl.AccountInfo', on_delete=models.CASCADE)

    objects = MessageManager()

    class Meta:
        db_table = 'message'
        verbose_name_plural = verbose_name = '留言'
        ordering = ['-created_time']

    def __str__(self):
        return self.message[:20]
